package top.baselitch.liteorm;

import android.util.Log;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;

public class LiteDaoFactory{
    @SuppressWarnings("unchecked")
    public static <T> T create(Class<T> daoClass){
        Utils.verifyDaoInterface(daoClass);
        return (T) Proxy.newProxyInstance(daoClass.getClassLoader(),new Class<?>[]{daoClass},new MyInvocation());
    }
    private static class MyInvocation implements InvocationHandler{
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return buildDaoExecutor(method,loadDaoMehod(method,args));
        }
        private DaoMethod loadDaoMehod(Method method,Object[] args){
            return new DaoMethod.Builder(method,args).builder();
        }

        private DaoExecutor buildDaoExecutor(Method method,DaoMethod daoMethod){
            Class<?> returnClass = method.getReturnType();
            Type returnType = method.getGenericReturnType();
            DaoExecutor<?> daoExecutor = null;
            if(returnType == DaoExecutor.class){
                parseMethodReturn(returnType);
                daoExecutor = new DaoExecutor(LiteORM.getInstance().getDbHelper(),daoMethod);
            }else{
                throw new IllegalArgumentException("this reutrn not DaoExecutor");
            }
            return daoExecutor;
        }

        private void parseMethodReturn(Type returnType){
        }
    }

}
